const PositionDao = require("../dao/position-dao.js");
const PositionService = {
	add(req,res,next){
		const {position,company,exprience,type,address,salary} = req.body;
		// 上传文件的路径字符串
		let logo = "";
		if (req.file)
			logo = "/images/upload/" + req.file.filename;
		console.log(req.body);
		// 保存到数据库中
		PositionDao.save({position,company,exprience,type,address,salary,logo})
				   .then(data=>{
				   		console.log(data)
					   	res.json({
			   				res_code:1,
			   				res_error:"",
			   				res_body:{
			   					status:1,
			   					data:data
			   				}
			   			})
				   })
				   .catch(err=>{
				   		res.json({
			   				res_code:1,
			   				res_error:err,
			   				res_body:{
			   					status:0,
			   				}
			   			})
				   })
	},
	find(req,res,next){
		PositionDao.find()
					.then(data=>{
						//console.log(data);不加条件find出来是包含数据库里所有doc的数组
						res.json({
			   				res_code:1,
			   				res_error:"",
			   				res_body:{
			   					status:1,
			   					data:data
			   				}
		   				})		
					})
					.catch(err=>{
						res.json({
			   				res_code:1,
			   				res_error:err,
			   				res_body:{
			   					status:0,
			   				}
			   			})
					})
	},
	delete(req,res,next){
		console.log(req.body);
		const {position} = req.body;
		PositionDao.del({position})
					.then(data=>{
						console.log(data);
						res.json({
							res_code:1,
			   				res_error:"",
			   				res_body:{
			   					status:1,
			   					data:data
							}
						})
					})
					.catch(err=>{
						res.json({
							res_code:1,
			   				res_error:err,
			   				res_body:{
			   					status:0,
			   				}
						})
					})
	},
	edit(req,res,next){
		const {position,company,exprience,type,address,salary,_id} = req.body;
		const dd = {position,company,exprience,type,address,salary,_id};
		if(req.file)
			dd.logo = "/images/upload/" + req.file.filename;
		PositionDao.update({_id:dd._id},dd)
					.then(data=>{
						res.json({
							res_code:1,
			   				res_error:"",
			   				res_body:{
			   					status:1,
			   					data:data
			   				}
						})
					})
					.catch(err=>{
						res.json({
							res_code:1,
			   				res_error:err,
			   				res_body:{
			   					status:0,
			   				}
						})
					})
	}
}

module.exports = PositionService;